package com.bmos.mybatisplus.generatorui;

import com.bmos.mybatisplus.generatorui.mbp.NameConverter;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;

import java.util.Map;


/**
 * 不使用@SpringBootApplication和@EnableAutoConfiguration
 * 注解，避免启动时被宿主系统的自动配置所干扰，直接注入需要的配置类
 */
@SpringBootApplication
// @EnableAutoConfiguration
// @Import({
//         DispatcherServletAutoConfiguration.class,
//         ServletWebServerFactoryAutoConfiguration.class,
//         HttpEncodingAutoConfiguration.class,
//         HttpMessageConvertersAutoConfiguration.class,
//         MultipartAutoConfiguration.class,
//         ErrorMvcAutoConfiguration.class,
//         WebMvcAutoConfiguration.class})
@Slf4j
public class MybatisPlusToolsApplication {

    private static GeneratorConfig generatorConfig;

    public static void main(String[] args) {
        GeneratorConfig config = GeneratorConfig.builder().jdbcUrl("jdbc:mysql://localhost:3306/xxl_job?useUnicode" +
                        "=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai")
                .userName("root")
                .password("admin")
                .port(8888)
                .driverClassName("com.mysql.cj.jdbc.Driver")
                // 生成代码存放包路径
                .basePackage("com.bmos.mybatisplus.generatorui.xxl")
                // 数据库表前缀，生成entity名称时会去掉
                // .tablePrefix("t_")
                .nameConverter(new NameConverter() {
                    @Override
                    public String serviceNameConvert(String entityName) {
                        return entityName + "Service";
                    }

                    @Override
                    public String controllerNameConvert(String entityName) {
                        return entityName + "Controller";
                    }
                })
                .build();
        run(config);
    }

    public static void run(GeneratorConfig generatorConfig) {
        if (Strings.isNullOrEmpty(generatorConfig.getJdbcUrl())) {
            throw new IllegalArgumentException("jdbcUrl必须要设置");
        }
        MybatisPlusToolsApplication.generatorConfig = generatorConfig;
        Map<String, Object> props = Maps.newHashMap();
        new SpringApplicationBuilder()
                .properties(props)
                .sources(MybatisPlusToolsApplication.class)
                .run();
    }

    @Bean
    public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> containerConfig(GeneratorConfig config) {
        return factory -> {
            // 注释掉端口读取的代码，使用配置方式设置端口
            // if (config.getPort() != null) {
            //     factory.setPort(MybatisPlusToolsApplication.generatorConfig.getPort());
            // } else {
            //     factory.setPort(8080);
            // }
            factory.setContextPath("");
        };
    }

    @Bean
    public GeneratorConfig generatorConfig() {
        return MybatisPlusToolsApplication.generatorConfig;
    }


}

